package com.dxy.df.modules.controller;

import java.util.List;

import com.dxy.df.core.assit.SearchHandler;
import com.dxy.df.core.model.PageRs;
import com.dxy.df.core.model.Rs;
import com.dxy.df.modules.es.IEsSearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.dxy.df.modules.bean.entity.DfArticleEntity;
import com.dxy.df.modules.bean.vo.DfArticleVo;
import com.dxy.df.modules.bean.query.DfArticleQuery;
import com.dxy.df.modules.bean.put.DfArticlePut;
import com.dxy.df.modules.service.DfArticleService;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.validation.Valid;
/**
 * 文章表
 * @author biz@dxy.cn
 * @date 2019-10-25 16:23:46
 */
@Api(tags="文章表")
@RestController
@RequestMapping("/api/article")
public class DfArticleController {

	@Autowired
	private DfArticleService dfArticleService;
	@Autowired
	private SearchHandler searchHandler;

	@ApiOperation(value = "文章表分页列表")
	@PostMapping("/search")
	public PageRs<DfArticleVo> search(@RequestBody @Valid DfArticleQuery query){
		//查询列表数据
		List<DfArticleVo> voList =  dfArticleService.queryList(query);
		int total = dfArticleService.queryTotal(query);
		return searchHandler.search(voList,total,query);
	}


	@ApiOperation(value = "文章表信息")
	@GetMapping("/info/{id}")
	public Rs<DfArticleEntity> info(@PathVariable("id") Integer id){
		DfArticleQuery query = new DfArticleQuery();
		query.setId(id);
		DfArticleEntity dfArticle = dfArticleService.queryObject(query);

		return Rs.ok().putData(dfArticle);
	}

	@ApiOperation(value = "文章表新增")
	@PostMapping
	public Rs save(@RequestBody DfArticlePut dfArticle){
		dfArticleService.save(dfArticle);
		return Rs.ok();
	}

	@ApiOperation(value = "文章表修改")
	@PutMapping
	public Rs update(@RequestBody DfArticlePut dfArticle){
		dfArticleService.update(dfArticle);
		return Rs.ok();
	}

	@ApiOperation(value = "文章表删除")
	@DeleteMapping
	public Rs delete(@RequestParam Integer[] ids){
		dfArticleService.deleteBatch(ids);
		return Rs.ok();
	}

	@ApiOperation(value = "文章明细查询")
	@GetMapping("/{id}/detail")
	public Rs queryDetail(@PathVariable("id") Integer id){
		dfArticleService.updateCount(id);
		return Rs.ok().putData(dfArticleService.queryDetail(id));
	}

	@ApiOperation(value = "热文")
	@GetMapping("/top")
	public Rs top10(){
		return Rs.ok().putData(dfArticleService.selectTop());
	}


	@ApiOperation(value = "热文")
	@GetMapping("/es/{value}")
	public Rs es(@PathVariable String value){
		return Rs.ok().putData(dfArticleService.esSearch(value));
	}


}
